#!/bin/zsh

# Creates an Apple framework for the given platform type
# documentation: https://developer.apple.com/documentation/bundleresources/placing_content_in_a_bundle
#                https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPFrameworks/Concepts/FrameworkAnatomy.html
echo "⌛️ Creating MaterialX.framework ..."

# Variables are substituted by CMake
CMAKE_INSTALL_PREFIX="@CMAKE_INSTALL_PREFIX@"
PROJECT_BINARY_DIR="@PROJECT_BINARY_DIR@"

FRAMEWORK_NAME="MaterialX"
FRAMEWORK_DIR="${CMAKE_INSTALL_PREFIX}/frameworks/${FRAMEWORK_NAME}.framework"
FRAMEWORK_LIBRARIES_DIR="${FRAMEWORK_DIR}/Libraries"
FRAMEWORK_ROOT_LIBRARY_NAME="libMaterialX.@MATERIALX_LIBRARY_VERSION@.dylib"
EMBEDDED_BUILD=@__embedded_build@
FRAMEWORK_RESOURCES_DIR="${FRAMEWORK_DIR}"
MATERIALX_SOURCE_LIBRARIES="${CMAKE_INSTALL_PREFIX}/libraries/"
BUNDLE_IDENTIFIER="org.aswf.materialx"
CODESIGN_ID="@CMAKE_XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY@"
OLD_RC_PATH="${CMAKE_INSTALL_PREFIX}/lib"

# Remove the existing directory if it exists
if [ -d ${FRAMEWORK_DIR} ]; then
  echo "Removing existing framework";
  rm -Rf ${FRAMEWORK_DIR};
fi

# Create the parent directory
echo "Creating Framework Directory: ${FRAMEWORK_DIR}"
mkdir -p ${FRAMEWORK_DIR}

if [ "$EMBEDDED_BUILD" = true ];then
  FRAMEWORK_RESOURCES_DIR="${FRAMEWORK_DIR}/Assets"
  FRAMEWORK_PLIST_LOCATION="${FRAMEWORK_DIR}/Info.plist"
  FRAMEWORK_HEADERS_DIR="${FRAMEWORK_DIR}/Headers"
  FRAMEWORK_LIB_PATH=""${FRAMEWORK_DIR}/${FRAMEWORK_NAME}""
  FRAMEWORK_LINKER_PATH="@rpath/Frameworks/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}"
else
  FRAMEWORK_RESOURCES_DIR="${FRAMEWORK_DIR}/Versions/A/Resources/"
  FRAMEWORK_PLIST_LOCATION="${FRAMEWORK_DIR}/Versions/A/Resources/Info.plist"
  FRAMEWORK_HEADERS_DIR="${FRAMEWORK_DIR}/Versions/A/Headers"
  FRAMEWORK_LIB_PATH="${FRAMEWORK_DIR}/Versions/A/${FRAMEWORK_NAME}"
  FRAMEWORK_LINKER_PATH="@rpath/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}"
fi

echo "Creating Resources Root: ${FRAMEWORK_RESOURCES_DIR}"
mkdir -p ${FRAMEWORK_RESOURCES_DIR}

echo "Creating Headers Root: ${FRAMEWORK_HEADERS_DIR}"
mkdir -p ${FRAMEWORK_HEADERS_DIR}

# Copy the plist over
echo "Copying files into ${FRAMEWORK_DIR}"
ditto "${PROJECT_BINARY_DIR}/Info.plist" "${FRAMEWORK_PLIST_LOCATION}"

# Copy the primary directories over
ditto "${CMAKE_INSTALL_PREFIX}/include/" ${FRAMEWORK_HEADERS_DIR}
ditto "${CMAKE_INSTALL_PREFIX}/libraries/" "${FRAMEWORK_RESOURCES_DIR}/libraries"
ditto "${CMAKE_INSTALL_PREFIX}/resources" "${FRAMEWORK_RESOURCES_DIR}/render"

cp "${CMAKE_INSTALL_PREFIX}/lib/${FRAMEWORK_ROOT_LIBRARY_NAME}" "${FRAMEWORK_LIB_PATH}"

# Setup symlinks
if [ "$EMBEDDED_BUILD" = false ];then
  (cd "${FRAMEWORK_DIR}/Versions" && ln -s "A" "Current")
  (cd ${FRAMEWORK_DIR} && ln -s "Versions/Current/Resources" "Resources")
  (cd ${FRAMEWORK_DIR} && ln -s "Versions/Current/Headers" "Headers")
  (cd ${FRAMEWORK_DIR} && ln -s "Versions/Current/${FRAMEWORK_NAME}" ${FRAMEWORK_NAME})
fi

# Fix the linkage on the primary dylib
install_name_tool -id "${FRAMEWORK_LINKER_PATH}" "${FRAMEWORK_DIR}/${FRAMEWORK_NAME}"
install_name_tool -change "@rpath/libMaterialX.@MATERIALX_MAJOR_VERSION@.dylib" "${FRAMEWORK_LINKER_PATH}" "${FRAMEWORK_DIR}/${FRAMEWORK_NAME}"

# Frameworks require all includes to use the framework name as the prefix for automatic discovery
echo "Modifying headers..."
HEADER_SET=""
for i in $(cd $FRAMEWORK_HEADERS_DIR && ls -d */ | cut -f1 -d'/');do
  HEADER_SET+="${i}|"
done;
# Sed on macOS is POSIX compliant and so uses different args than GNU
# Things to be aware of are [[:<:]] and [[:>:]] are for word boundaries and spaces are explicit literals
INCLUDE_PATTERN="^# *include [\"|<]([[:<:]](${HEADER_SET::-1})[[:>:]].*)[\"|>].*$"
find ${FRAMEWORK_HEADERS_DIR} -type f -name "*.h*" -print0 | xargs -0 sed -i "" -E "s,${INCLUDE_PATTERN},#include <${FRAMEWORK_NAME}/\1>,g"

# Sign the final framework
echo "Codesigning the framework..."
set -e
codesign --force --sign ${CODESIGN_ID} --generate-entitlement-der --identifier ${BUNDLE_IDENTIFIER} --verbose ${FRAMEWORK_DIR}

echo "✅ Finished creating framework at ${FRAMEWORK_DIR}"
